(function() {
    window.onload = function() {
      var placeMarkers = [];
      var map, selfMarker, options, center, lastZoomLevel;
      var homeLatitude, homeLongitude, homeLatLng;
      var lastLatitude, lastLongitude, lastLatLng;

      homeLatitude = utils.readCookie('homeLatitude');
      homeLongitude = utils.readCookie('homeLongitude');
      homeLatLng = new google.maps.LatLng(homeLatitude, homeLongitude);

      //lastLatitude = utils.readCookie('lastLatitude') || homeLatitude;
      //lastLongitude = utils.readCookie('lastLongitude') || homeLongitude;
      //lastLatLng = new google.maps.LatLng(lastLatitude, lastLongitude);

      center = homeLatLng;
      lastZoomLevel = parseInt(utils.readCookie('lastZoomLevel'), 10) || 10;

      options = {
        center: center,
        zoom: lastZoomLevel,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableDefaultUI: true,
        navigationControl: true,
        navigationControlOptions: {
          position: google.maps.ControlPosition.RIGHT_BOTTOM,
          style: google.maps.NavigationControlStyle.Aj
        }
      };

      map = new google.maps.Map(document.getElementById('big-map'), options);

      google.maps.event.addListener(map, 'zoom_changed', function() {
          var zoomLevel = map.getZoom();
          utils.createCookie('lastZoomLevel', zoomLevel, 365);
          console.log('lastZoomLevel: ' + zoomLevel);
      });

      selfMarker = gmap.createSelfMarker(map, lastLatLng || homeLatLng);                             
      utils.writeLatLng(lastLatitude || homeLatitude, lastLongitude || homeLongitude);

      console.log('map has been created!');
      google.maps.event.addListener(selfMarker, 'dragend', function() {
          selfMarker.setOptions({icon: gmap.selfMarker});
          var lastLatLng = selfMarker.getPosition();
          var expires = new Date(Date.now() + gmap.ONE_DAY * 365).toUTCString();
          lastLatitude = lastLatLng.lat();
          lastLongitude = lastLatLng.lng();
          console.log('lastLatidue: ' + lastLatitude);
          console.log('lastLongitude: ' + lastLongitude);
          console.log('start set cookie');
          utils.createCookie('lastLatitude', lastLatitude);
          utils.createCookie('lastLongitude', lastLongitude);
          console.log(lastLatitude);
          console.log(lastLongitude);
          console.log('end set cookie');
          map.setCenter(lastLatLng);

          utils.writeLatLng(lastLatitude, lastLongitude);
      });                                                                               

      $('#home-button').click(function() {
          console.log('back to home');
          map.setCenter(homeLatLng);
          selfMarker.setPosition(homeLatLng);
          lastLatitude = homeLatitude;
          lastLongitude = homeLongitude;
          utils.createCookie('lastLatitude', lastLatitude);
          utils.createCookie('lastLongitude', lastLongitude);
      });                           

      $('#search-button').click(function() {
          var keyword = $('#search').val();

          // Do ajax request
          $.getJSON('/place/search/', 
            {keyword:keyword,latitude:lastLatitude,longitude:lastLongitude},
            function (places) {
              gmap.deleteOverlays(placeMarkers);
              for (var i = 0; i < places.length; i++) {
                placeMarkers.push(gmap.createPlaceMarker(map, places[i]));
              }
          });
      });
    };
})();
